<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

session_start();

class Roots extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		
		$this->load->helper(array('url','cookie','html','language','security'));
		$this->load->library(array('parser','session','user_agent','upload','encrypt', 'image_lib'));
		
		$uagent = do_hash($this->agent->browser());
		$platform = do_hash($this->agent->platform());
		
		if($this->agent->is_browser())
		{
			if(($this->input->cookie('dsn_agent') != $uagent) OR ($this->input->cookie('dsn_platform') != $platform))
			{
				redirect(base_url('error/uagent'));
				exit(0);
			}
		}else{
			redirect(base_url('error/uagent'));
			exit(0);
		}
		
		require "./lib/src/skey.php";
		
		if(($uid != 1) OR ($lvl != 1))
		{
			redirect(base_url('main/login.html'));
			exit(0);
		}
		
		$this->load->model('Admin_model','adminmodel',TRUE);	
			
		function pesan($pesan, $url)
		{
			echo "
				<script type='text/javascript'>
					alert('$pesan');
					window.location.href='$url';
				</script>
				<noscript>
					<center>
						$pesan <br />
						<a href='$url'>Lanjutkan &gt;&gt;</a>
					</center>
				</noscript>
			";
		}
		
	}
	   
	public function index()
	{
		$this->load->view('main/login');
	}
	
	
	public function kategoribaru()
	{
		$kategori = $this->input->post('kategori');
		$insert = FALSE;
		
		$cekkat = $this->adminmodel->get_single_data('kategori', 'dn_category', 'kategori', $kategori);
		if($cekkat !== FALSE)
		{
			pesan('Kategori sudah ada!', base_url('otakkanan/kategori'));
			exit(0);
		}
		
		if( ! empty($kategori))
		{
			$data = array('idkategori'=>'', 'kategori'=>$kategori);
			$insert = $this->adminmodel->set_kategori('dn_category',$data);
		}
		
		if($insert === TRUE)
		{
			pesan('Berhasil Menambah Kategori baru!',base_url('otakkanan/kategori'));
		}else{
			pesan('Gagal Menambah Kategori!',base_url('otakkanan/kategori'));
		}
		
	}
	
	public function updatekategori()
	{
		$idkat = $this->input->post('idkategori');
		$kategori = $this->input->post('kategori');
		
		$update = FALSE;
		
		$cekkat = $this->adminmodel->get_single_data('kategori', 'dn_category', 'kategori', $kategori);
		if($cekkat !== FALSE)
		{
			pesan('Kategori sudah ada!', base_url('otakkanan/kategori/edit?idkat='.$idkat));
			exit(0);
		}
		
		if(! empty($idkat) && ! empty($kategori))
		{
			$data = array('kategori'=>$kategori);
			$update = $this->adminmodel->update_kategori($data, $idkat);
			
		}else{
			pesan('Data Tidak lengkap',base_url('otakkanan/kategori'));
		}
		
		if($update === TRUE)
		{
			pesan('Sukses Memperbaharui Kategori', base_url('otakkanan/kategori'));
		}else{
			pesan('Gagal Memperbaharui Kategori', base_url('otakkanan/kategori'));
		}
	}
	
	public function hapuskategori()
	{
		$idkat = $this->input->get('idkat');
		
		$hapus = $this->adminmodel->hapus_kategori($idkat);
		
		if($hapus === TRUE)
		{
			pesan('Berhasil Menghapus Kategori.', base_url('otakkanan/kategori'));
		}else{
			pesan('Gagal Menghapus Kategori.', base_url('otakkanan/kategori'));
		}
	}
	
	public function usahabaru()
	{
		$nama = $this->input->post('nama');
		$kategori = $this->input->post('kategori');
		$pemilik = $this->input->post('pemilik');
		$deskripsi = $this->input->post('deskripsi');
		$alamat = $this->input->post('alamat');
		$kota = $this->input->post('kota');
		$situs = prep_url(strtolower($this->input->post('situs')));
		$email = $this->input->post('email');
		$phone = $this->input->post('phone');
		$vphone = $this->input->post('vphone');
		$gambar = "";
		
		$config['upload_path'] = './uploads/perusahaan/';
		$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
		$config['max_size']	= '1000';
		$config['max_width'] = '1000';
		$config['max_height'] = '1000';
		$config['max_filename'] = '100';
		$this->upload->initialize($config);
		
		$insert = FALSE;
		
		if((!empty($nama)) && (!empty($kategori)) && (!empty($pemilik)) && (!empty($deskripsi)) && (!empty($alamat)) && (!empty($kota)))
		{
			
			if( ! empty($_FILES['logo']['name']))
			{
				if ($this->upload->do_upload('logo'))
				{
					$udata = $this->upload->data();
					$gambar = $udata['file_name'];
				}else{
					pesan('Terjadi Kesalahan saat mengunggah gambar\nPastikan ekstensi gambar diperbolehkan dan ukuran kurang dari 1 MB',base_url('otakkanan/perusahaan/baru'));
					exit(0);
				}
			}
			
			$data = array('idperusahaan'=>'','idkategori'=>$kategori,'uid' => $uid,'nama'=>$nama,'pemilik'=>$pemilik,'deskripsi'=>$deskripsi,'alamat'=>$alamat,'kota'=>$kota,'situs'=>$situs, 'email'=>$email,'telp'=>$phone,'telpv'=>$vphone,'logo'=>$gambar, 'status' => TRUE);
			
			$insert = $this->adminmodel->set_usaha($data);
			
		}else{
			pesan('Data Tidak Lengkap',base_url('otakkanan/perusahaan/baru'));
			exit(0);
		}
		
		if($insert === TRUE)
		{
			if( ! empty($gambar))
			{
				$config['image_library'] = 'gd2';
				$config['source_image']	= './uploads/perusahaan/'.$gambar;
				$config['create_thumb'] = FALSE;
				$config['maintain_ratio'] = FALSE;
				$config['width']	 = 600;
				$config['height']	= 240;

				$this->image_lib->initialize($config); 

				$this->image_lib->resize();
			}
			
			pesan('Berhasil Menambah Data baru',base_url('otakkanan/perusahaan'));
		}else{
			pesan('Gagal Menambah Data',base_url('otakkanan/perusahaan'));
		}
		
	}
	
	public function updateusaha()
	{
		$idc = $this->input->post('idc');
		$nama = $this->input->post('nama');
		$kategori = $this->input->post('kategori');
		$pemilik = $this->input->post('pemilik');
		$deskripsi = $this->input->post('deskripsi');
		$alamat = $this->input->post('alamat');
		$kota = $this->input->post('kota');
		$situs = prep_url(strtolower($this->input->post('situs')));
		$email = $this->input->post('email');
		$phone = $this->input->post('phone');
		$vphone = $this->input->post('vphone');
		$gambar = $this->input->post('oldgambar');
		$oldgambar = $gambar;
		
		$config['upload_path'] = './uploads/perusahaan/';
		$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
		$config['max_size']	= '1000';
		$config['max_width'] = '1000';
		$config['max_height'] = '1000';
		$config['max_filename'] = '100';
		$this->upload->initialize($config);
		
		$deloldgambar = FALSE;
		$update = FALSE;
		
		
		if((!empty ($idc)) && (!empty($nama)) && (!empty($pemilik)) && (!empty($deskripsi)) && (!empty($alamat)) && (!empty($kota)))
		{
			
			if(!empty($_FILES['logo']['name']))
			{
				if ($this->upload->do_upload('logo'))
				{
					$udata = $this->upload->data();
					$gambar = $udata['file_name'];
					$deloldgambar = TRUE;
				}else{
					pesan('Terjadi Kesalahan saat mengunggah gambar\nPastikan ekstensi gambar diperbolehkan dan ukuran kurang dari 1 MB',base_url('otakkanan/perusahaan/edit?idc='.$idc));
					exit(0);
				}
			}
			
			$data = array('idkategori'=>$kategori,'nama'=>$nama,'pemilik'=>$pemilik,'deskripsi'=>$deskripsi,'alamat'=>$alamat,'kota'=>$kota,'situs'=>$situs, 'email'=>$email,'telp'=>$phone,'telpv'=>$vphone,'logo'=>$gambar);
			
			$update = $this->adminmodel->update_usaha($data, $idc);
			
		}else{
			pesan('Data Tidak lengkap',base_url('otakkanan/perusahaan'));
		}
		
		if($update === TRUE)
		{
			if( ! empty ($oldgambar) && ($deloldgambar == TRUE))
			{
				unlink('./uploads/perusahaan/'.$oldgambar);
				
				$config['image_library'] = 'gd2';
				$config['source_image']	= './uploads/perusahaan/'.$gambar;
				$config['create_thumb'] = FALSE;
				$config['maintain_ratio'] = FALSE;
				$config['width']	 = 600;
				$config['height']	= 240;

				$this->image_lib->initialize($config); 

				$this->image_lib->resize();
			}
			pesan('Sukses Memperbaharui Data',base_url('otakkanan/perusahaan'));
		}else{
			pesan('Gagal Memperbaharui Data',base_url('otakkanan/perusahaan'));
		}
	}
	
	public function suspendusaha()
	{
		$idusaha = $this->input->get('idc');
		$act = $this->input->get('act');
		
		$data = array('status' => FALSE);
		$pesan_sukses = "Perusahaan telah dibekukan!";
		$pesan_gagal = "Gagal Membekukan perusahaan!";
		
		if($act == "open")
		{
			$data = array('status' => TRUE);
			$pesan_sukses = "Proses Berhasil!";
			$pesan_gagal = "Proses Gagal!";
		}
		
		if($idusaha == 1)
		{
			$suspend = FALSE;
		}else{
			$suspend = $this->adminmodel->update_usaha($data, $idusaha);
		}
		
		if($suspend === TRUE)
		{
			pesan($pesan_sukses, base_url('otakkanan/perusahaan'));
		}
		else{
			pesan($pesan_gagal, base_url('otakkanan/perusahaan'));
		}
	}
	
	public function hapususaha()
	{
		$idusaha = $this->input->get('idc');
		$logo = $this->adminmodel->get_single_data('gambar','dn_company','idperusahaan',$idusaha);
		
		if($idusaha == 1)
		{
			$hapus = FALSE;
		}else{
			$hapus = $this->adminmodel->hapus_usaha('dn_company','idperusahaan',$idusaha);
			
		}
	
		if($hapus === TRUE)
		{
			if( ! empty($logo))
			{
				unlink('./uploads/perusahaan/'.$logo);
			}
			pesan('Berhasil Menghapus Data',base_url('otakkanan/perusahaan'));
		}else{
			pesan('Gagal Menghapus Data',base_url('otakkanan/perusahaan'));
		}
	}
	
	public function produkbaru()
	{
		$nama = $this->input->post('nama');
		$jenis = $this->input->post('jenis');
		$deskripsi = $this->input->post('deskripsi');
		$idc = $this->input->post('idc');
		$gambar = "";
		$insert = FALSE;
		
		if(empty($idc))
		{
			$idc = 1;
		}
		
		$config['upload_path'] = './uploads/produk/';
		$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
		$config['max_size']	= '1000';
		$config['max_width'] = '1000';
		$config['max_height'] = '2000';
		$config['max_filename'] = '100';
		$this->upload->initialize($config);

		if((!empty($nama)) && (!empty($deskripsi)))
		{
			if(!empty($_FILES['gambar']['name']))
			{
				if ($this->upload->do_upload('gambar'))
				{
					$udata = $this->upload->data();
					$gambar = $udata['file_name'];
				}else{
					pesan('Terjadi Kesalahan saat mengunggah gambar\nPastikan ekstensi gambar diperbolehkan dan ukuran kurang dari 1 MB',base_url('otakkanan/produk/baru'));
					exit(0);
				}
			}
			
			$data = array('idproduk'=>'','idperusahaan'=>$idc,'nama'=>$nama,'deskripsi'=>$deskripsi,'gambar'=>$gambar);
			$insert = $this->adminmodel->set_produk($data);
			
		}else{
			pesan('Data Tidak Lengkap', base_url('otakkanan/produk/baru'));
		}
		
		if($insert === TRUE)
		{
			pesan('Sukses Menambah Data',base_url('otakkanan/produk/'));
		}else{
			pesan('Terjadi Kesalahan saat memproses data',base_url('otakkanan/produk/baru?idc='.$idc));
		}
		
	}
	
	public function updateproduk()
	{
		$nama = $this->input->post('nama');
		$jenis = $this->input->post('jenis');
		$deskripsi = $this->input->post('deskripsi');
		$gambar = $this->input->post('oldgambar');
		$oldgambar = $gambar;
		$idp = $this->input->post('idp');
		
		$deloldgambar = FALSE;
		$update = FALSE;
		
		$config['upload_path'] = './uploads/produk/';
		$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
		$config['max_size']	= '1000';
		$config['max_width'] = '1000';
		$config['max_height'] = '2000';
		$config['max_filename'] = '100';
		$this->upload->initialize($config);
		
		if((!empty ($idp)) && (!empty($nama)) && (!empty($deskripsi)) )
		{
			
			if(!empty($_FILES['gambar']['name']))
			{
				if ($this->upload->do_upload('gambar'))
				{
					$udata = $this->upload->data();
					$gambar = $udata['file_name'];
					$deloldgambar = TRUE;
					
				}else{
					pesan('Terjadi Kesalahan saat mengunggah gambar\nPastikan ekstensi gambar diperbolehkan dan ukuran kurang dari 1 MB',base_url('otakkanan/produk/'));
					exit(0);
				}
			}
			
			$data = array('nama'=>$nama,'deskripsi'=>$deskripsi,'gambar'=>$gambar);
			$update = $this->adminmodel->update_produk($data, $idp);			
			
		}else{
			pesan('Data Tidak lengkap',base_url('otakkanan/produk'));
		}
		
		if($update === TRUE)
		{
			if( ! empty ($oldgambar) && ($deloldgambar == TRUE))
			{
				unlink('./uploads/produk/'.$oldgambar);
			}
			pesan('Sukses Memperbaharui Data',base_url('otakkanan/produk'));
		}else{
			pesan('Gagal Memperbaharui Data',base_url('otakkanan/produk'));
		}
	}
	
	public function hapusproduk()
	{
		$idproduk = $this->input->get('idp');
		
		$gambar = $this->adminmodel->get_single_data('gambar','dn_product','idproduk',$idproduk);
		$hapus = $this->adminmodel->hapus_produk($idproduk);
	
		if($hapus === TRUE)
		{
			if(! empty($gambar))
			{
				unlink('./uploads/produk/'.$gambar);
			}
			pesan('Berhasil Menghapus Data',base_url('otakkanan/produk'));
		}else{
			pesan('Gagal Menghapus Data',base_url('otakkanan/produk'));
		}
	}
	
	public function tambahloker()
	{
		$posisi = $this->input->post('posisi');
		$deskripsi = $this->input->post('deskripsi');
		$tipe = $this->input->post('tipe');
		$lokasi = $this->input->post('lokasi');
		$cara = $this->input->post('cara');
		$berakhir = $this->input->post('berakhir');
		$idc = $this->input->post('idc');
		
		$gambar = "";
		$insert = FALSE;
		
		if(empty($idc))
		{
			$idc = 1;
		}
		
		$config['upload_path'] = './uploads/lowongan/';
		$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
		$config['max_size']	= '1000';
		$config['max_width'] = '1000';
		$config['max_height'] = '2000';
		$config['max_filename'] = '100';
		$this->upload->initialize($config);
		
		if((!empty($posisi)) && (!empty($deskripsi)) && (!empty($lokasi)) && (!empty($cara)) && (!empty($berakhir)))
		{
			if(!empty($_FILES['gambar']['name']))
			{
				if ($this->upload->do_upload('gambar')){
					$udata = $this->upload->data();
					$gambar = $udata['file_name'];
				}else{
					pesan('Terjadi Kesalahan saat mengunggah gambar\nPastikan ekstensi gambar diperbolehkan dan ukuran kurang dari 1 MB',base_url('otakkanan/loker/baru'));
					exit(0);
				}
			}
			
			$data = array('idjob'=>'','idperusahaan'=>$idc,'posisi'=>$posisi,'deskripsi'=>$deskripsi,'tipe'=>$tipe,'lokasi'=>$lokasi,'cara'=>$cara,'gambar'=>$gambar,'berakhir'=>$berakhir);
			$insert = $this->adminmodel->set_loker($data);
			if($insert === TRUE){
				pesan('Sukses Menambah Data',base_url('otakkanan/loker/'));
			}else{
				pesan('Gagal Menambah Data',base_url('otakkanan/loker/baru'));
			}
			
		}else{
			pesan('Data Tidak Lengkap',base_url('otakkanan/loker/baru'));
		}
		
		
	}
	
	public function updateloker()
	{
		$posisi = $this->input->post('posisi');
		$deskripsi = $this->input->post('deskripsi');
		$tipe = $this->input->post('tipe');
		$lokasi = $this->input->post('lokasi');
		$cara = $this->input->post('cara');
		$berakhir = $this->input->post('berakhir');
		$gambar = $this->input->post('oldgambar');
		$oldgambar = $gambar;
		$idl = $this->input->post('idl');
		
		$update = FALSE;
		$deloldgambar = FALSE;
		
		$config['upload_path'] = './uploads/lowongan/';
		$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
		$config['max_size']	= '1000';
		$config['max_width'] = '1000';
		$config['max_height'] = '2000';
		$config['max_filename'] = '100';
		$this->upload->initialize($config);
		
		if((!empty ($idl)) && (!empty($posisi)) && (!empty($deskripsi)) && (!empty($lokasi)) && (!empty($cara)) && (!empty($berakhir)))
		{
			
			if(!empty($_FILES['gambar']['name']))
			{
				if ($this->upload->do_upload('gambar'))
				{
					$udata = $this->upload->data();
					$gambar = $udata['file_name'];
					$deloldgambar = TRUE;
					
				}else{
					pesan('Terjadi Kesalahan saat mengunggah gambar\nPastikan ekstensi gambar diperbolehkan dan ukuran kurang dari 1 MB',base_url('otakkanan/loker/'));
					exit(0);
				}
			}
			$data = array('posisi'=>$posisi,'deskripsi'=>$deskripsi,'tipe'=>$tipe,'lokasi'=>$lokasi,'cara'=>$cara,'gambar'=>$gambar,'berakhir'=>$berakhir);
			$update = $this->adminmodel->update_loker($data, $idl);
			
		}else{
			pesan('Data Tidak lengkap',base_url('otakkanan/loker'));
		}
		
		if($update === TRUE)
		{
			if( ! empty ($oldgambar) && ($deloldgambar == TRUE))
			{
				unlink('./uploads/lowongan/'.$oldgambar);
			}
					
			pesan('Sukses Memperbaharui Data',base_url('otakkanan/loker'));
		}else{
			pesan('Gagal Memperbaharui Data',base_url('otakkanan/loker'));
		}
		
		
	}
	
	public function hapusloker()
	{
		$idloker = $this->input->get('idl');
		
		$gambar = $this->adminmodel->getsingle_data_('gambar','dn_job','idjob',$idloker);
		$hapus = $this->adminmodel->hapus_loker($idloker);
	
		if($hapus === TRUE)
		{
			if( ! empty($gambar))
			{
				unlink('./uploads/lowongan/'.$gambar);
			}
			pesan('Berhasil Menghapus Data',base_url('otakkanan/loker'));
		}else{
			pesan('Gagal Menghapus Data',base_url('otakkanan/loker'));
		}
	}
	
	
	public function penggunabaru()
	{
		$nama = $this->input->post('nama');
		$email = $this->input->post('email');
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$password2 = $this->input->post('password2');
		$jk = $this->input->post('jk');
		$tgllahir = $this->input->post('tgllahir');
		$level = $this->input->post('level');
		$sectoken = substr(md5(microtime()),0,16);
		
		$insert = FALSE;
		
		if((!empty($nama)) && (!empty($email)) && (!empty($username)) && (!empty($password)) && (!empty($password2)) && (!empty($level)))
		{
			
			$cekuser = $this->adminmodel->cek_user_name($username);
			if($cekuser !== TRUE)
			{
				if($password == $password2)
				{
					$newpass = do_hash($password);
					
					$data = array('uid' => '','username' => $username,'password' => $newpass,'nama' => $nama,'email' => $email,'jk' => $jk,'tgllahir' => $tgllahir,'tglreg' => date("Y-m-d"),'gambar' => '','sectoken' => $sectoken,'level'=>$level, 'status' => TRUE);
					$insert = $this->adminmodel->set_pengguna($data);
					
					if($insert === TRUE)
					{
						pesan('Sukses Menambah Data',base_url('otakkanan/pengguna/'));
					}else{
						pesan('Gagal menambah Data',base_url('otakkanan/pengguna/baru'));
					}
				}else{
					pesan('Kedua Password Tidak sama',base_url('otakkanan/pengguna/baru'));
					
				}
			}else{
				pesan('Username sudah Digunakan\nSilakan Gunakan Username Lain',base_url('otakkanan/pengguna/baru'));
			}
			
		}else{
			pesan('Data Tidak Terkirim',base_url('otakkanan/pengguna/baru'));
		}
		
	}
	
	public function updatepengguna()
	{
		
		$usrid = $this->input->post('uid');
		$nama = $this->input->post('nama');
		$email = $this->input->post('email');
		$password = $this->input->post('password');
		$password2 = $this->input->post('password2');
		$jk = $this->input->post('jk');
		$tgllahir = $this->input->post('tgllahir');
		$level = $this->input->post('level');
		$status = $this->input->post('status');
		$gambar = $this->input->post('oldgambar');
		$oldgambar = $gambar;
		$sectoken = substr(md5(microtime()),0,16);
		
		$update = FALSE;
		$deloldgambar = FALSE;
		
		$config['upload_path'] = './uploads/pengguna/';
		$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
		$config['max_size']	= '1000';
		$config['max_width'] = '2000';
		$config['max_height'] = '1000';
		$config['max_filename'] = '100';
		$this->upload->initialize($config);
		
		if($usrid == 1)
		{
			$level = 1;
			$jk = TRUE;
			$status = TRUE;
		}
		
		if(!empty($password))
		{
			if($password == $password2)
			{
				$newpass = do_hash($password);
					if(!empty($_FILES['gambar']['name']))
					{
						if($this->upload->do_upload('gambar'))
						{
							$udata = $this->upload->data();
							$gambar = $udata['file_name'];
							$deloldgambar = TRUE;
						}else{
							pesan('Kesalahan Saat mengunggah gambar.\nPastikan ektensi gambar anda diperkenankan serta ukuran gambar tidak melebihi 1 MB',base_url('otakkanan/pengguna/'));
							exit(0);
						}
					}
				$data = array('password'=>$newpass,'nama'=>$nama,'email'=>$email,'jk'=>$jk,'tgllahir'=>$tgllahir,'gambar'=>$gambar,'sectoken' => $sectoken,'level'=>$level, 'status' => $status);
			
			}else{
				pesan('Password Tidak Sama',base_url('otakkanan/pengguna/edit?uid='.$uid));
				exit(0);
			}
		}else{
			if(!empty($_FILES['gambar']['name']))
			{
				if($this->upload->do_upload('gambar'))
				{
					$udata = $this->upload->data();
					$gambar = $udata['file_name'];
					$deloldgambar = TRUE;
				}else{
					pesan('Kesalahan Saat mengunggah gambar.\nPastikan ektensi gambar anda diperkenankan serta ukuran gambar tidak melebihi 1 MB',base_url('otakkanan/pengguna/'));
					exit(0);
				}
			}
			$data = array('nama'=>$nama,'email'=>$email,'jk'=>$jk,'tgllahir'=>$tgllahir,'gambar'=>$gambar,'level'=>$level, 'status' => $status);
		}
		
		$update = $this->adminmodel->update_pengguna($data, $usrid);
		
		if($update === TRUE)
		{
			if( ! empty ($oldgambar) && ($deloldgambar == TRUE))
			{
				unlink('./uploads/pengguna/'.$oldgambar);
			}
			pesan('Sukses memperbaharui pengguna',base_url('otakkanan/pengguna/'));
		}else{
			pesan('Gagal Memperbaharui data',base_url('otakkanan/pengguna/'));
		}
		
	}
	
	public function hapuspengguna()
	{
		$usrid = $this->input->get('uid');
		if($usrid == 1)
		{
			$hapus = FALSE;
		}else{
			$gambar = $this->adminmodel->get_single_data('gambar','dn_users','uid',$usrid);
			$hapus = $this->adminmodel->hapus_pengguna($usrid);
		}
		
		if($hapus === TRUE)
		{
			if( ! empty($gambar))
			{
				unlink('./uploads/pengguna/'.$gambar);
			}
			pesan('Berhasil menghapus data',base_url('otakkanan/pengguna/'));
		}else{
			pesan('Gagal Menghapus Data',base_url('otakkanan/pengguna/'));
		}
		
	}
	
	public function updateprofil()
	{
		include "./lib/src/skey.php";
		
		$nama = $this->input->post('nama');
		$email = $this->input->post('email');
		$password = $this->input->post('password');
		$password2 = $this->input->post('password2');
		$jk = $this->input->post('jk');
		$tgllahir = $this->input->post('tgllahir');
		$level = $this->input->post('level');
		$status = $this->input->post('status');
		$gambar = $this->input->post('oldgambar');
		$oldgambar = $gambar;
		$sectoken = substr(md5(microtime()),0,16);
		$mypassword = $this->input->post('mypassword');
		
		$update = FALSE;
		$deloldgambar = FALSE;
		
		$config['upload_path'] = './uploads/pengguna/';
		$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
		$config['max_size']	= '1000';
		$config['max_width'] = '2000';
		$config['max_height'] = '1000';
		$config['max_filename'] = '100';
		$this->upload->initialize($config);
		
		$cekpass = $this->adminmodel->cek_user_password($uid, do_hash($mypassword));
		if($cekpass !== TRUE)
		{
			pesan('Password saat ini tidak cocok',base_url('otakkanan/edit'));
			exit(0);
		}
		
		if(!empty($password))
		{
			if($password == $password2)
			{
				$newpass = do_hash($password);
					if(!empty($_FILES['gambar']['name']))
					{
						if($this->upload->do_upload('gambar'))
						{
							$udata = $this->upload->data();
							$gambar = $udata['file_name'];
							$deloldgambar = TRUE;
						}else{
							pesan('Kesalahan Saat mengunggah gambar.\nPastikan ektensi gambar anda diperkenankan serta ukuran gambar tidak melebihi 1 MB',base_url('otakkanan/edit'));
							exit(0);
						}
					}
				$data = array('password'=>$newpass,'nama'=>$nama,'email'=>$email,'jk'=>$jk,'tgllahir'=>$tgllahir,'gambar' => $gambar,'sectoken' => $sectoken);
			
			}else{
				pesan('Password Tidak Sama',base_url('otakkanan/edit'));
				exit(0);
			}
		}else{
			if(!empty($_FILES['gambar']['name']))
			{
				if($this->upload->do_upload('gambar'))
				{
					$udata = $this->upload->data();
					$gambar = $udata['file_name'];
					$deloldgambar = TRUE;
				}else{
					pesan('Kesalahan Saat mengunggah gambar.\nPastikan ektensi gambar anda diperkenankan serta ukuran gambar tidak melebihi 1 MB',base_url('otakkanan/edit'));
					exit(0);
				}
			}
			$data = array('nama'=>$nama,'email'=>$email,'jk'=>$jk,'tgllahir'=>$tgllahir,'gambar' => $gambar);
		}
		
		$update = $this->adminmodel->update_pengguna($data, $uid);
		
		if($update === TRUE)
		{
			if( ! empty ($oldgambar) && ($deloldgambar == TRUE))
			{
				unlink('./uploads/pengguna/'.$oldgambar);
			}
			pesan('Sukses memperbaharui pengguna',base_url('otakkanan/edit'));
		}else{
			pesan('Gagal Memperbaharui data',base_url('otakkanan/edit'));
		}
	}
	
	public function acarabaru()
	{
		$nama = $this->input->post('nama');
		$deskripsi = $this->input->post('deskripsi');
		$lokasi = $this->input->post('lokasi');
		$tanggal = $this->input->post('tanggal');
		
		$gambar = "";
		$insert = FALSE;
		
		include "./lib/src/skey.php";
		
		if(empty($uid))
		{
			pesan('Anda Tidak Memiliki Akses',base_url('otakkanan/acara'));
			exit(0);
		}
		
		$config['upload_path'] = './uploads/acara/';
		$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
		$config['max_size']	= '1000';
		$config['max_width'] = '1000';
		$config['max_height'] = '2000';
		$config['max_filename'] = '100';
		$this->upload->initialize($config);
		
		if((!empty($nama)) && (!empty($deskripsi)) && (!empty($lokasi)) && (!empty($tanggal)))
		{
			if( ! empty($_FILES['gambar']['name']))
			{
				if ($this->upload->do_upload('gambar')){
					$udata = $this->upload->data();
					$gambar = $udata['file_name'];
				}else{
					pesan('Terjadi Kesalahan saat mengunggah gambar\nPastikan ekstensi gambar diperbolehkan dan ukuran kurang dari 1 MB',base_url('otakkanan/promosi/baru'));
					exit(0);
				}
			}
			
			$data = array('idacara'=>'','uid'=>$uid,'nama'=>$nama,'deskripsi'=>$deskripsi,'tanggal'=>$tanggal,'gambar'=>$gambar,'lokasi'=>$lokasi);
			$insert = $this->adminmodel->set_acara($data);
			
		}else{
			pesan('Data Tidak Lengkap',base_url('otakkanan/acara/baru'));
		}
		
		if($insert == TRUE)
		{
			pesan('Sukses Menambah Data',base_url('otakkanan/acara/'));
		}
		else
		{
			pesan('Gagal Menambah Data',base_url('otakkanan/acara/baru'));
		}
		
	}
	
	public function updateacara()
	{
		$nama = $this->input->post('nama');
		$deskripsi = $this->input->post('deskripsi');
		$lokasi = $this->input->post('lokasi');
		$tanggal = $this->input->post('tanggal');
		$gambar = $this->input->post('oldgambar');
		$oldgambar = $gambar;
		$ida = $this->input->post('ida');
		
		$update = FALSE;
		$deloldgambar = FALSE;
		
		$config['upload_path'] = './uploads/acara/';
		$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
		$config['max_size']	= '1000';
		$config['max_width'] = '1000';
		$config['max_height'] = '1000';
		$config['max_filename'] = '100';
		$this->upload->initialize($config);
		
		if((!empty ($ida)) && (!empty($nama)) && (!empty($deskripsi)) && (!empty($lokasi)) && (!empty($tanggal)))
		{
			
			if( ! empty($_FILES['gambar']['name']))
			{
				if ($this->upload->do_upload('gambar'))
				{
					$udata = $this->upload->data();
					$gambar = $udata['file_name'];
					$deloldgambar = TRUE;
					
				}else{
					pesan('Terjadi Kesalahan saat mengunggah gambar\nPastikan ekstensi gambar diperbolehkan dan ukuran kurang dari 1 MB',base_url('otakkanan/acara/'));
					exit(0);
				}
			}
			$data = array('nama'=>$nama,'deskripsi'=>$deskripsi,'tanggal'=>$tanggal,'gambar'=>$gambar,'lokasi'=>$lokasi);
			$update = $this->adminmodel->update_acara($data, $ida);
			
		}else{
			pesan('Data Tidak lengkap',base_url('otakkanan/acara'));
		}
		
		if($update === TRUE)
		{
			if(! empty ($oldgambar) && ($deloldgambar == TRUE))
			{
				unlink('./uploads/acara/'.$oldgambar);
			}
			pesan('Sukses Memperbaharui Data',base_url('otakkanan/acara'));
		}else{
			pesan('Gagal Memperbaharui Data',base_url('otakkanan/acara'));
		}
	}
	
	public function hapusacara()
	{
		$idacara = $this->input->get('ida');
		
		$gambar = $this->adminmodel->get_single_data('gambar','dn_event','idacara',$idacara);
		$hapus = $this->adminmodel->hapus_acara($idacara);
	
		if($hapus === TRUE)
		{
			if( ! empty($gambar))
			{
				unlink('./uploads/acara/'.$gambar);
			}
			pesan('Berhasil Menghapus Data',base_url('otakkanan/acara'));
		}else{
			pesan('Gagal Menghapus Data',base_url('otakkanan/acara'));
		}
	}
	
	public function hapusmasukan()
	{
		$idm = $this->input->get('idm');
		
		$hapus = $this->adminmodel->hapus_masukan($idm);
		if($hapus === TRUE)
		{
			pesan('Sukses Menghapus Data',base_url('otakkanan/masukan'));
		}else{
			pesan('Gagal Menghapus Data',base_url('otakkanan/masukan'));
		}
		
	}
	
	public function hapuslaporan()
	{
		$idl = $this->get->input('id');
		
		$hapus = $this->adminmodel->hapus_laporan($idl);
		if($hapus === TRUE)
		{
			pesan('Sukses Menghapus Data',base_url('otakkanan/masukan'));
		}else{
			pesan('Gagal Menghapus Data',base_url('otakkanan/masukan'));
		}
	}
	
	public function scplugin()
	{
		include "./lib/src/skey.php";
		
		$sc = $this->input->get('sc');
		$act = $this->input->get('act');
		
		switch($sc)
		{
			case "fb":
				include "./lib/facebook/src/facebook.php";
				include "./lib/src/fbkey.php";
				
				$pesan = "";
				
				if($fbuser)
				{
					$cek = $this->adminmodel->cek_social_network($uid, 'fb');
					$cek2 = $this->adminmodel->cek_social_network_id($sc, $fbuser);
					
					switch($act){
						case "add":
							if($cek !== TRUE)
							{
								$akses = $this->adminmodel->set_social_network($uid, $sc, $fbuser);
								$pesan = "Berhasil Menautkan!";
							}else
							{
								$akses = $akses = $this->adminmodel->update_social_network($uid, $sc, $fbuser);
								$pesan = "Berhasil Menautkan!";
							}
						;
						break;
						
						case "upd":
							if($cek === TRUE)
							{
								$akses = $this->adminmodel->update_social_network($uid, $sc, $fbuser);
								$pesan = "Berhasil Mengubah data!";
							}
							else
							{
								$akses = FALSE;
							}
						;
						break;
						
						case "rmv":
							$akses = $this->adminmodel->update_social_network($uid, $sc, '');
							$pesan = "Akses Akun telah dihentikan";
						;
						break;
						
						default:
							pesan('Terjadi Kesalahan', base_url('otakkanan'));
						;
						break;
						
					}
					
					
					if($akses === TRUE)
					{
						pesan($pesan, base_url('otakkanan'));
					}
					else{
						pesan('Terjadi Kesalahan', base_url('otakkanan'));
					}
					
				}
				else{
					pesan('Terjadi Kesalahan', base_url('otakkanan'));
				}
			;
			break;
			
			case "tw":
				echo "Error not configure";
			;
			break;
			
			case "gp":
				
				require './lib/google/src/Google_Client.php';
				require './lib/google/src/contrib/Google_Oauth2Service.php';
		
				require './lib/src/googlekey.php';
				$pesan = "";
				if(isset ($gpuser))
				{
					$cek = $this->adminmodel->cek_social_network($uid, 'gp');
					
					switch($act){
						case "add":
							if($cek !== TRUE)
							{
								$akses = $this->adminmodel->set_social_network($uid, $sc, $gpuser['id']);
								$pesan = "Berhasil Menautkan!";
							}else
							{
								$akses = $akses = $this->adminmodel->update_social_network($uid, $sc, $gpuser['id']);
								$pesan = "Berhasil Menautkan!";
							}
						;
						break;
						
						case "upd":
							if($cek === TRUE)
							{
								$akses = $this->adminmodel->update_social_network($uid, $sc, $gpuser['id']);
								$pesan = "Berhasil Mengubah data!";
							}
							else
							{
								$akses = FALSE;
							}
						;
						break;
						
						case "rmv":
							$akses = $this->adminmodel->update_social_network($uid, $sc, '');
							$pesan = "Akses Akun telah dihentikan";
						;
						break;
						
						default:
							pesan('Terjadi Kesalahan', base_url('otakkanan'));
						;
						break;
						
					}
					
					
					if($akses === TRUE)
					{
						pesan($pesan, base_url('otakkanan'));
					}
					else{
						pesan('Terjadi Kesalahan', base_url('otakkanan'));
					}
				}
				else{
					pesan('Terjadi Kesalahan', base_url('otakkanan'));
				}
					
			;
			break;
			
			default:
				pesan('Terjadi Kesalahan', base_url('otakkanan'));
			;
			break;
		}
	}
	
	/*
	public function promosibaru(){
		$judul = $this->input->post('judul');
		$promosi = $this->input->post('promosi');
		$berakhir = $this->input->post('berakhir');
		$idc = $this->input->post('idc');
		
		$gambar = "";
		$insert = false;
		
		if(empty($idc)){
			$idc = 1;
		}
		
		$config['upload_path'] = './uploads/promosi/';
		$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
		$config['max_size']	= '1000';
		$config['max_width'] = '1000';
		$config['max_height'] = '1000';
		$config['max_filename'] = '100';
		$this->upload->initialize($config);

		
		
		if((!empty($judul)) && (!empty($promosi))){
			if(!empty($_FILES['gambar']['name'])){
				if ($this->upload->do_upload('gambar')){
					$udata = $this->upload->data();
					$gambar = $udata['file_name'];
				}else{
					pesan('Terjadi Kesalahan saat mengunggah gambar\nPastikan ekstensi gambar diperbolehkan dan ukuran kurang dari 1 MB',base_url('otakkanan/promosi/baru'));
					exit(0);
				}
			}
			
			$data = array('idpromosi'=>'','idperusahaan'=>$idc,'judul'=>$judul,'promosi'=>$promosi,'gambar'=>$gambar,'berakhir'=>$berakhir);
			$this->adminmodel->setData('dn_promote',$data);
			pesan('Sukses Menambah Data',base_url('otakkanan/promosi/'));
		}else{
			pesan('Data Tidak Lengkap',base_url('otakkanan/promosi/baru'));
		}
		
	}
	
	public function updatepromosi(){
		$judul = $this->input->post('judul');
		$promosi = $this->input->post('promosi');
		$berakhir = $this->input->post('berakhir');
		$gambar = $this->input->post('oldgambar');
		$idp = $this->input->post('idp');
		
		$update = false;
		
		$config['upload_path'] = './uploads/promosi/';
		$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
		$config['max_size']	= '1000';
		$config['max_width'] = '1000';
		$config['max_height'] = '1000';
		$config['max_filename'] = '100';
		$this->upload->initialize($config);
		
		if((!empty ($idp)) && (!empty($judul)) && (!empty($promosi)) && (!empty($berakhir))){
			
			if(!empty($_FILES['gambar']['name'])){
				if ($this->upload->do_upload('gambar')){
					if($gambar != ""){
						unlink('./uploads/promosi/'.$gambar);
					}
					
					$udata = $this->upload->data();
					$gambar = $udata['file_name'];
					
				}else{
					pesan('Terjadi Kesalahan saat mengunggah gambar\nPastikan ekstensi gambar diperbolehkan dan ukuran kurang dari 1 MB',base_url('otakkanan/promosi/'));
					exit(0);
				}
			}
			$data = array('judul'=>$judul,'promosi'=>$promosi,'gambar'=>$gambar,'berakhir'=>$berakhir);
			$update = $this->adminmodel->updateData('dn_promote',$data,'idpromosi',$idp);
			
		}else{
			pesan('Data Tidak lengkap',base_url('otakkanan/promosi'));
		}
		
		if($update == true){
			pesan('Sukses Memperbaharui Data',base_url('otakkanan/promosi'));
		}else{
			pesan('Gagal Memperbaharui Data',base_url('otakkanan/promosi'));
		}
		
		
	}
	
	public function hapuspromosi(){
		$idp = $this->input->get('idp');
		
		$gambar = $this->adminmodel->getDataSingle('gambar','dn_promote','idpromosi',$idp);
		$hapus = $this->adminmodel->delData('dn_promote','idpromosi',$idp);
	
		if($hapus == true){
			if(!empty($gambar)){
				unlink('./uploads/promosi/'.$gambar);
			}
			pesan('Berhasil Menghapus Data',base_url('otakkanan/promosi'));
		}else{
			pesan('Gagal Menghapus Data',base_url('otakkanan/promosi'));
		}
	}
	public function kontak(){
		$nama = $this->input->post('nama');
		$email = $this->input->post('email');
		$masukan = $this->input->post('masukan');
		$privatekey = "6Le4eNMSAAAAAO2HSWUNnFqlvrksaJVgPsdK6uUr";
		
		# the response from reCAPTCHA
		$resp = null;
		# the error code from reCAPTCHA, if any
		$error = null;
		
		if(($nama != null) && ($email != null) && ($masukan != null)){
			
			$this->load->view('lib/recaptchalib',TRUE);
			
			# was there a reCAPTCHA response?
			if ($_POST["recaptcha_response_field"]) {
					$resp = recaptcha_check_answer ($privatekey,
													$_SERVER["REMOTE_ADDR"],
													$_POST["recaptcha_challenge_field"],
													$_POST["recaptcha_response_field"]);

				if ($resp->is_valid) {
						$this->load->library('email');
						
						$this->email->from('no-reply@dadannetwork.com');
						$this->email->to('dadan.setia@gmail.com');

						$this->email->subject('Kontak konsumen DadanNetwork');
						$this->email->message($masukan);	

						$this->email->send();
						
						$this->email->from('no-reply@dadannetwork.com');
						$this->email->to($email);

						$this->email->subject('Terima Kasih');
						$this->email->message('Bpk/ibu/saudara/saudari '.$nama.' Pertanyaan, saran, ide kritik, masukan anda akan kami tinjau, Terima Kasih sebelumnya.');	

						$this->email->send();

						echo $this->email->print_debugger();
						
						
						pesan('Terima kasih \nKami akan segera meninjaunya',base_url('main/'));
						
				} else {
						# set the error code so that we can display it
						pesan('Captcha tidak cocok',base_url('main/kontak'));
				}
			}
			
			
		}else{
			pesan('Data Tidak Lengkap',base_url('main/kontak'));
		}
		
	}
	*/
	
	
	
	
	
	
	
}

/* End of file otakkiri.php */
/* Location: ./application/controllers/otakkiri.php */
